import javax.swing.tree.TreeNode;

public class MySingleList {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void disPlay() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int data) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void clear() {
        this.head = null;
    }

    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    public void addLast(int data) {
        if (head == null) {
            ListNode node = new ListNode(data);
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        ListNode node = new ListNode(data);
        cur.next = node;
    }

    public ListNode searchCur(ListNode prev, int data) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return cur;
            }
            prev = cur;
            cur = cur.next;
        }
        return null;
    }

    public void remove(int key) {
        ListNode prev = null;
        ListNode cur = searchCur(prev, key);
        if (cur == null) {
            System.out.println("没有要删除的节点");
            return;
        }
        prev.next = cur.next;
    }

    public void removeAll(int key) {
        ListNode prev = null;
        ListNode cur = head;
        if (head == null) {
            System.out.println("节点为空");
            return;
        }
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
    }

    public static void main(String[] args) {
        MySingleList my = new MySingleList();
        my.addLast(1);
        my.addLast(11);
        my.addLast(2);
        my.addLast(12);
        my.addLast(13);
        my.addLast(31);
        my.disPlay();
        my.addLast(2);
        my.disPlay();
        my.addIndex(3,66);
        my.disPlay();
        my.removeAll(2);
        my.disPlay();
    }
}